import Vue from "vue";
import Router from "vue-router";
import Layout from "@/layout";
import { Message } from "element-ui";
import store from "../store";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import getPageTitle from "@/utils/get-page-title";

Vue.use(Router);

// 全量路由表
export const routes = [
	{
		path: "/login",
		component: () => import("@/views/login/index"),
		hidden: true
	},
	{
		path: "/",
		component: Layout,
		redirect: "/dashboard",
		children: [
			{
				path: "dashboard",
				name: "Dashboard",
				component: () => import("@/views/dashboard/index"),
				meta: { title: "首页", icon: "el-icon-s-home" }
			}
		]
	},
	{
		path: "/fileComparison",
		component: Layout,
		children: [
			{
				path: "",
				name: "FileComparison",
				component: () => import("@/views/fileComparison/index"),
				meta: { title: "文件对比", icon: "el-icon-files" }
			}
		]
	},
	{
		path: "/esComparison",
		component: Layout,
		children: [
			{
				path: "",
				name: "ESComparison",
				component: () => import("@/views/esComparison/index"),
				meta: { title: "ES对比", icon: "el-icon-position" }
			}
		]
	},
	{
		path: "/project",
		component: Layout,
		meta: { title: "项目应用", icon: "el-icon-s-data" },
		children: [
			{
				path: "melon",
				name: "Melon",
				component: () => import("@/views/project/melon/index"),
				meta: { title: "智能补调", icon: "el-icon-postcard" }
			},
			{
				path: "forecast",
				name: "Forecast",
				component: () => import("@/views/project/forecast/index"),
				meta: { title: "销量预测", icon: "el-icon-set-up" }
			},
			{
				path: "simstock",
				name: "SimStock",
				component: () => import("@/views/project/simstock/index"),
				meta: { title: "库存仿真", icon: "el-icon-monitor" }
			}
		]
	},
	{
		path: "/404",
		component: Layout,
		hidden: true,
		children: [
			{
				path: "",
				name: "404",
				component: () => import("@/views/404.vue"),
				meta: { title: "找不到页面" }
			}
		]
	},
	{
		path: "*",
		redirect: "/404",
		hidden: true
	}
];
// 创建路由实例
const router = new Router({
	mode: "hash",
	routes: routes,
	scrollBehavior(to, from, savePosition) {
		return savePosition ? savePosition : { x: 0, y: 0 };
	}
});
// 路由守卫
router.beforeEach(async (to, from, next) => {
	NProgress.start();
	document.title = getPageTitle(to.meta.title);
	// 判断是否登录
	const hasToken = store.getters.token;
	if (hasToken) {
		if (to.path === "/login") {
			// 登录成功，重定向到首页
			next({ path: "/" });
			NProgress.done();
		} else {
			const hasGetUserInfo = store.getters.name;
			if (hasGetUserInfo) {
				next();
			} else {
				try {
					await store.dispatch("user/getInfo");
					next();
				} catch (error) {
					await store.dispatch("user/resetToken");
					Message.error(error || "Has Error");
					next(`/login?redirect=${to.path}`);
					NProgress.done();
				}
			}
		}
	} else {
		// 没有token只能进入或重定向到登录页
		const whiteList = ["/login"];
		if (whiteList.indexOf(to.path) !== -1) {
			next();
		} else {
			next(`/login?redirect=${to.path}`);
			NProgress.done();
		}
	}
});

router.afterEach(() => {
	NProgress.done();
});

export default router;
